class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        //预处理前缀积数组
        f[0] = 1;
        for (int i = 1; i < n; i++) {
            f[i] = f[i - 1] * nums[i - 1];
        }
        //预处理后缀积数组
        g[n - 1] = 1;
        for (int i = n - 1 - 1; i >= 0; i--) {
            g[i] = g[i + 1] * nums[i + 1];
        }
        //使用前后缀积数组
        int[] ret = new int[n];
        for (int i = 0; i < n; i++) {
            ret[i] = f[i] * g[i];
        }
        return ret;
    }
}